﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
namespace SocketTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Client client = new Client();
            client.runClient();
        }

     
    }
    class Client
    {
        string answer; //string that stores the answer
        System.IO.StreamReader streamReader; //used for input from server
        System.IO.StreamWriter streamWriter; //used for output to server
        Thread currentQuestion; //a thread that runs the current question. Is replaced if a new quesiton comes in



    
        public void runClient()
        {
            TcpClient serverSocket;
            try
            {
                serverSocket = new TcpClient("localHost", 9001);
            }
            catch
            {
                Console.WriteLine("Failed to connect to server");
                return;
            }
            streamReader = new System.IO.StreamReader(serverSocket.GetStream());
            streamWriter = new System.IO.StreamWriter(serverSocket.GetStream());
            answer = "";
            //variables to store information on the question
            string question;
            string a;
            string b;
            string c;
            string d;
            Question tempQuestion; //used to send Question to thread
            string input;

            //first send the server your name
            Console.Write("Enter Your Name:");
            input = Console.ReadLine();
            streamWriter.WriteLine(input);
            streamWriter.Flush();
            currentQuestion = new Thread(newQuestion);


            while (true)
            {
                question = streamReader.ReadLine();
                a = streamReader.ReadLine();
                b = streamReader.ReadLine();
                c = streamReader.ReadLine();
                d = streamReader.ReadLine();

                //abort the previous thread if the player did not answer the previous question
                if (currentQuestion.IsAlive)
                {
                    
                    currentQuestion.Abort();
                    
                   
                    
                
                }


                //start a new thread for the next question
                currentQuestion = new Thread (newQuestion);
                tempQuestion= new Question();
                tempQuestion.question =question;
                tempQuestion.choices =new string[] {a,b,c,d};
                currentQuestion.Start(tempQuestion);
            }
        }

        private void newQuestion(object o)
        {
            Question q=(Question)o;
            string question = q.question;
            string a = q.choices[0];
            string b = q.choices[1];
            string c = q.choices[2];
            string d = q.choices[3];
            
            Console.WriteLine(question);
            Console.WriteLine("a. {0}\nb. {1}\nc. {2}\nd. {3}", a, b, c, d);
            if (answer == "") 
            answer = Console.ReadLine();
            streamWriter.WriteLine(answer);
            streamWriter.Flush();
            Console.WriteLine("Waiting for next question");
            answer = "";
        }
    }

    class Question
    {
        public string question;
        public string[] choices;
        public string answer;
    }
}
